/* 
* Express.js Request&Response示例
*/

//导入Express模块
const express=require("express");
//创建Express应用
const app=express();
//Express http服务端口
const port=3001;

//内置中间件:解析json数据
app.use(express.json());
//内置中间件:url解析
app.use(express.urlencoded({extended:false}));

//路由
app.get("/:req_id",function(req,res){
    console.log(req.params);
    console.log(req.query);

    let str=`路由参数(params)：${req.params.req_id}`;
    str+=`请求参数(query)：${req.query.key1}`

    res.send(str);
});

//请求数据
app.post("/",function(req,res){
    console.log(req.body);
    console.log(req.route);

    let str=`请求数据(body)：${req.body.msg}\n`;
    str+=`${req.ip}-${req.url}-${req.route.path}`;

    res.send(str);
});

//http访问路径
app.get("/",function(req,res){
    res.send("<h1>使用Express.js框架</h1>")
});

//启动Express服务
app.listen(port,function(){
    console.log(`MyApp访问地址：http://127.0.0.1:${port}`);
});